<template>
  <div class="edu-page-warp">
    <div class="tab" v-if="this.tabs.length > 0">
      <div v-for="(t,index) in tabs">
        <span :class="tabFocus==t.val?'current':''" @click="changeTab(t.val)">{{t.text}}</span>
        <span v-if="index!=tabs.length-1" class="split">|</span>
      </div>
    </div>
    <div style="clear:both"></div>
    <!--目录查询-->
    <div v-if="tabFocus==0">
      <condition-card @reset="reset" @search="onSubmit">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('professions.disciplineClass')">
              <el-select v-model="queryForm.levelOneIdList" @change="changeCategory" multiple collapse-tags clearable
                         filterable>
                <el-option
                  v-for="item in options.categories"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('professions.firstDiscipline')">
              <el-select v-model="queryForm.levelTwoIdList" multiple collapse-tags clearable filterable>
                <el-option
                  v-for="item in options.queryOneLevels"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title" style="margin-bottom:16px">{{$t('professions.nationalDisciplineCatalogueQuery')}}
        </div>
        <div class="edu-opertion-bar" style="margin-bottom:16px">
          <div class="float-left">
            <el-input v-model="queryForm.condition" :placeholder="$t('professions.plsInputSecondDisciplineCodeOrName')"
              style="width:280px" @keyup.enter.native="onSubmit">
              <i class="el-icon-search" slot="suffix" style="margin-top:10px;cursor:pointer" @click="onSubmit"></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-button type="primary" @click="handleExport">{{$t("message.export")}}</el-button>
          </div>
        </div>
        <el-table stripe :data="viewData" size="mini" width="100%" :key="Math.random()" tooltip-effect="light">
          <edu-table-column :label="$t('professions.disciplineClassCode')" prop="levelOneCode"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.disciplineClassName')" prop="levelOneName"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.disciplineClassNameEn')" prop="levelOneNameEn"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.firstDisciplineCode')" prop="levelTwoCode"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.firstDisciplineName')" prop="levelTwoName"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.firstDisciplineNameEn')" prop="levelTwoNameEn"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.secondDisciplineCode')" prop="code" :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.secondDisciplineName')" prop="name" :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.secondDisciplineNameEn')" prop="nameEn" :render-header="labelHead"/>
        </el-table>
        <el-row>
          <el-col>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryForm.pageNum_"
              :page-sizes="[20,50,100,500,1000]"
              :page-size="queryForm.pageSize_"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryForm.total_"
              style="float:right;margin-top:10px"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <!--门类-->
    <div v-if="tabFocus==1">
      <el-card shadow="never">
        <div class="edu-page-title" style="margin-bottom:16px">
          {{$t('professions.nationalDisciplineClassMaintenance')}}
        </div>
        <div class="edu-opertion-bar" style="margin-bottom:16px">
          <div class="float-left">
            <el-input
              v-model="queryForm.condition"
              :placeholder="$t('professions.plsInputDisciplineClassCodeOrName')"
              style="width:280px" clearable
              @keyup.enter.native="onSubmit"
            >
              <i
                class="el-icon-search"
                slot="suffix"
                style="margin-top:10px;cursor:pointer"
                @click="onSubmit"
              ></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-dropdown @command="importSubjectRecord">
              <el-button>
                {{$t('message.import')}}
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu :disabled="importButton" slot="dropdown">
                <el-dropdown-item command="in">{{$t('message.import')}}</el-dropdown-item>
                <el-dropdown-item command="out">{{$t('message.down')}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <!--   <el-button @click="downSubjectFiles">{{$t('message.down')}}</el-button>
              <el-button @click="importSubjectFiles">{{$t("message.import")}}</el-button> -->
            <el-button type="primary" @click="create">{{$t('professions.addPro')}}</el-button>
          </div>
        </div>

        <el-table stripe :data="viewData" size="mini" width="100%" :key="Math.random()" tooltip-effect="light">
          <edu-table-column :label="$t('professions.disciplineClassCode')" prop="code" :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.disciplineClassName')" prop="name" :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.disciplineClassNameEn')" prop="nameEn" :render-header="labelHead"/>
          <edu-table-column :label="$t('message.operating')">
            <template slot-scope="props">
              <el-button
                type="text"
                size="mini"
                @click="commonEdit(props.row)"
              >{{$t("message.edit")}}
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="commonDelete(props.row)"
              >{{$t("message.delete")}}
              </el-button>
            </template>
          </edu-table-column>
        </el-table>
        <el-row>
          <el-col>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryForm.pageNum_"
              :page-sizes="[20,50,100,500,1000]"
              :page-size="queryForm.pageSize_"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryForm.total_"
              style="float:right;margin-top:10px"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <!--一级目录-->
    <div v-if="tabFocus==2">
      <condition-card @reset="reset" @search="onSubmit">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('professions.disciplineClass')">
              <el-select v-model="queryForm.parentIdList" multiple collapse-tags clearable filterable>
                <el-option
                  v-for="item in options.categories"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('professions.isValid')">
              <el-select v-model="queryForm.status" clearable>
                <el-option
                  v-for="item in options.valids"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title" style="margin-bottom:16px">
          {{$t('professions.nationalFirstDisciplineMaintenance')}}
        </div>
        <div class="edu-opertion-bar" style="margin-bottom:16px">
          <div class="float-left">
            <el-input
              v-model="queryForm.condition"
              :placeholder="$t('professions.plsInputFirstDisciplineCodeOrName')"
              style="width:280px"
              @keyup.enter.native="onSubmit"
            >
              <i
                class="el-icon-search"
                slot="suffix"
                style="margin-top:10px;cursor:pointer"
                @click="onSubmit"
              ></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-dropdown @command="importFristRecord">
              <el-button>
                {{$t('message.import')}}
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu :disabled="importButton" slot="dropdown">
                <el-dropdown-item command="in">{{$t('message.import')}}</el-dropdown-item>
                <el-dropdown-item command="out">{{$t('message.down')}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <!--  <el-button @click="downFristFiles">{{$t('message.down')}}</el-button>
             <el-button @click="importFristFiles">{{$t("message.import")}}</el-button> -->
            <el-button type="primary" @click="create">{{$t('professions.addPro')}}</el-button>
          </div>
        </div>
        <el-table stripe :data="viewData" size="mini" width="100%" :key="Math.random()" tooltip-effect="light">
          <edu-table-column :label="$t('professions.disciplineClassCode')" prop="parentCode"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.disciplineClassName')" prop="parentName"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.firstDisciplineCode')" prop="code" :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.firstDisciplineName')" prop="name" :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.firstDisciplineNameEn')" prop="nameEn" :render-header="labelHead"/>
          <edu-table-column :label="$t('message.operating')">
            <template slot-scope="props">
              <el-button
                type="text"
                size="mini"
                @click="commonEdit(props.row)"
              >{{$t("message.edit")}}
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="commonDelete(props.row)"
              >{{$t("message.delete")}}
              </el-button>
            </template>
          </edu-table-column>
        </el-table>
        <el-row>
          <el-col>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryForm.pageNum_"
              :page-sizes="[20,50,100,500,1000]"
              :page-size="queryForm.pageSize_"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryForm.total_"
              style="float:right;margin-top:10px"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <!--二级目录-->
    <div v-if="tabFocus==3">
      <condition-card @reset="reset" @search="onSubmit">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('professions.firstDiscipline')">
              <el-select v-model="queryForm.parentIdList" multiple filterable collapse-tags clearable>
                <el-option
                  v-for="item in options.queryOneLevels"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('professions.isValid')">
              <el-select v-model="queryForm.status" clearable>
                <el-option
                  v-for="item in options.valids"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title" style="margin-bottom:16px">
          {{$t('professions.nationalSecondDisciplineMaintenance')}}
        </div>
        <div class="edu-opertion-bar" style="margin-bottom:16px">
          <div class="float-left">
            <el-input
              v-model="queryForm.condition"
              :placeholder="$t('professions.plsInputSecondDisciplineCodeOrName')"
              style="width:280px"
              @keyup.enter.native="onSubmit"
            >
              <i
                class="el-icon-search"
                slot="suffix"
                style="margin-top:10px;cursor:pointer"
                @click="onSubmit"
              ></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-dropdown @command="importSecondRecord">
              <el-button>
                {{$t('message.import')}}
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu :disabled="importButton" slot="dropdown">
                <el-dropdown-item command="in">{{$t('message.import')}}</el-dropdown-item>
                <el-dropdown-item command="out">{{$t('message.down')}}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <!-- <el-button @click="downSecondFiles">{{$t('message.down')}}</el-button>
            <el-button @click="importSecondFiles">{{$t("message.import")}}</el-button> -->
            <el-button type="primary" @click="create">{{$t('professions.addPro')}}</el-button>
          </div>
        </div>
        <el-table stripe :data="viewData" size="mini" width="100%" tooltip-effect="light">
          <edu-table-column :label="$t('professions.firstDisciplineCode')" prop="parentCode"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.firstDisciplineName')" prop="parentName"
                            :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.secondDisciplineCode')" prop="code" :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.secondDisciplineName')" prop="name" :render-header="labelHead"/>
          <edu-table-column :label="$t('professions.secondDisciplineNameEn')" prop="nameEn" :render-header="labelHead"/>
          <edu-table-column :label="$t('message.operating')">
            <template slot-scope="props">
              <el-button
                type="text"
                size="mini"
                @click="commonEdit(props.row)"
              >{{$t("message.edit")}}
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="commonDelete(props.row)"
              >{{$t("message.delete")}}
              </el-button>
            </template>
          </edu-table-column>
        </el-table>
        <el-row>
          <el-col>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryForm.pageNum_"
              :page-sizes="[20,50,100,500,1000]"
              :page-size="queryForm.pageSize_"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryForm.total_"
              style="float:right;margin-top:10px"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <!--门类对话框-->
    <el-dialog :title="dialogTitle" width="550px" :visible.sync="showCategoryDialog" @close="resetFormValidate()" :close-on-click-modal="false">
      <el-form :model="postForm" label-position="right" label-width="150px" :rules="rules" ref="postForm1" label-suffix="：">
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.disciplineClassCode')" prop="code">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="postForm.code" maxlength="32"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.disciplineClassName')" prop="name">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="postForm.name" maxlength="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.disciplineClassNameEn')" prop="nameEn">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="postForm.nameEn" maxlength="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('message.remark')" prop="remark">
              <el-input :placeholder="$t('common.pla.pleaseInput')" type="textarea" rows="4" v-model="postForm.remark" maxlength="255"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="float-right">
              <el-button @click="cancel">{{$t("message.cancel")}}</el-button>
              <el-button type="primary" @click="commonSave">{{$t("message.save")}}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!--一级学科对话框-->
    <el-dialog :title="dialogTitle" width="550px" :visible.sync="showFirstLevelDialog" @close="resetFormValidate()" :close-on-click-modal="false">
      <el-form :model="postForm" label-position="right" label-width="150px" :rules="rules" ref="postForm2" label-suffix="：">
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.disciplineClass')" prop="parentId">
              <el-select v-model="postForm.parentId" style="width: 100%;">
                <el-option :placeholder="$t('common.pla.pleaseSelect')" v-for="item in options.categories" :key="item.value"
                  :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.firstDisciplineCode')" prop="code">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="postForm.code" maxlength="32"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.firstDisciplineName')" prop="name">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="postForm.name" maxlength="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.firstDisciplineNameEn')" prop="nameEn">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="postForm.nameEn" maxlength="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.isValid')" prop="status">
              <el-select v-model="postForm.status" clearable style="width: 100%;">
                <el-option v-for="item in options.valids" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('message.remark')" prop="remark">
              <el-input type="textarea" rows="4" v-model="postForm.remark" maxlength="255"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="float-right">
              <el-button @click="cancel">{{$t("message.cancel")}}</el-button>
              <el-button type="primary" @click="commonSave">{{$t("message.save")}}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!--二级学科对话框-->
    <el-dialog :title="dialogTitle" width="500px" :visible.sync="showSecondLevelDialog" @close="resetFormValidate()" :close-on-click-modal="false">
      <el-form :model="postForm" label-position="right" label-width="150px" :rules="rules" ref="postForm3" label-suffix="：">
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.disciplineClass')" prop="root">
              <el-select v-model="postForm.root" style="width: 100%;" @change="changeFormCategory">
                <el-option :placeholder="$t('common.pla.pleaseSelect')" v-for="item in options.categories" :key="item.value"
                  :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.firstDiscipline')" prop="parentId">
              <el-select v-model="postForm.parentId" style="width: 100%;">
                <el-option :placeholder="$t('common.pla.pleaseSelect')" v-for="item in options.FormOneLevels" :key="item.value"
                  :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.secondDisciplineCode')" prop="code">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="postForm.code" maxlength="32"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.secondDisciplineName')" prop="name">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="postForm.name" maxlength="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.secondDisciplineNameEn')" prop="nameEn">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="postForm.nameEn" maxlength="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('professions.isValid')" prop="status">
              <el-select v-model="postForm.status" clearable style="width: 100%;">
                <el-option v-for="item in options.valids" :key="item.value" :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :offset="3" :span="18">
            <el-form-item :label="$t('message.remark')" prop="remark">
              <el-input :placeholder="$t('common.pla.pleaseInput')" type="textarea" rows="4" v-model="postForm.remark" maxlength="255"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="float-right">
              <el-button @click="cancel">{{$t("message.cancel")}}</el-button>
              <el-button type="primary" @click="commonSave">{{$t("message.save")}}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <!-- 导入页面 -->
    <edu-dialog :title="uploadTittle" width="500px" ref="uploadDialog" @close="clearFiles" @confirm="uploadFile">
      <el-form label-width="85px">
        <el-row>
          <el-form-item>
            <el-upload ref="upload" :data="{}" :on-success="uploadDone" :before-upload="beforeAvatarUpload" :before-remove="beforeRemove"
              :on-error="onError" :auto-upload="false" :multiple="false" :on-change="(file, fileList)=>{this.fileList = fileList}"
              :limit="1" :accept="'application/vnd.ms-excel'" :action="importUrl">
              <el-button slot="trigger" size="small" type="primary">{{$t('label.selectFile')}}</el-button>
              <div slot="tip" class="el-upload__tip">
                {{$t('common.upload.uploadXLSFileOnly')}},{{$t('common.upload.sizeLimit3M')}}
              </div>
            </el-upload>
          </el-form-item>
        </el-row>
      </el-form>
    </edu-dialog>

    <!-- 导入提示对话框 -->
    <edu-export-dialog :tip="$t('common.upload.scienceDegreeMajor')" @close="closeFresh" ref="importRef" :data="importCam">
      <div :style="fontStyle">{{msg}}</div>
      <div style="margin-top: 4px;font-size: 12px;color: #909399">{{errorInfo1}}</div>
      <div style="margin-top: 4px;font-size: 12px;color: #909399">{{errorInfo2}}</div>
    </edu-export-dialog>
  </div>
</template>
<script>
    import bus from "common/src/utils/bus";
    import {queryDic} from "common/src/api/dictionary";
    import UTILS from "common/src/utils/utils";
    import {
        addMajor,
        getMajorList,
        getMajorById,
        editMajor,
        deleteMajor,
        getAllMajorList,
        exportMajor,
        getReportStatus,
        downloadSubjectUrl,
        downloadFristUrl,
        downloadSecondUrl,
        getimportStatus
    } from "common/src/api/resource/course";
    import mixin from 'common/src/mixins/index';

    export default {
        name: "scienceDegreeMajor",
        mixins: [mixin],
        data() {
            return {
                dialogTitle: '',
                msg: '',
                errorInfo1: '',
                errorInfo2: '',
                importCam: null,
                importButton: false,
                fontStyle: 'font-size: 15px;color: #606266;',
                type: 1,
                tabs: [
                    {
                        text: this.$t('professions.nationalDisciplineCatalogueQuery'),
                        val: 0
                    },
                    {
                        text: this.$t('professions.nationalDisciplineClassMaintenance'),
                        val: 1
                    },
                    {
                        text: this.$t('professions.nationalFirstDisciplineMaintenance'),
                        val: 2
                    },
                    {
                        text: this.$t('professions.nationalSecondDisciplineMaintenance'),
                        val: 3
                    }
                ],
                queryForm: {
                    level: null,
                    condition: "",
                    levelOneId: null,
                    levelOneIdList: [],
                    levelTwoId: null,
                    levelTwoIdList: [],
                    parentId: null,
                    parentIdList: [],
                    status: null,
                    keyword: "",
                    pageNum_: 1,
                    pageSize_: 20,
                    total_: 0
                },
                postForm: {
                    code: "",
                    name: "",
                    nameEn: "",
                    parentId: "",
                    level: "",
                    remark: "",
                    status: "",
                    remarkEn: "",
                    root: ""
                },
                options: {
                    categories: [],
                    queryOneLevels: [],
                    FormOneLevels: [],
                    valids: [
                        {label: this.$t("message.yes"), value: 1},
                        {label: this.$t("message.no"), value: 0}
                    ]
                },
                rules: {
                    code: [
                        {
                            required: true,
                            message: this.$t('professions.plsInputCode'),
                            trigger: "blur"
                        }
                    ],
                    name: [
                        {
                            required: true,
                            message: this.$t('professions.plsInputName'),
                            trigger: "blur"
                        }
                    ],
                    nameEn: [
                        {
                            required: true,
                            message: this.$t('professions.plsInputNameEn'),
                            trigger: "blur"
                        }
                    ],
                    status: [
                        {
                            required: true,
                            message: this.$t('professions.plsChooseValidStatus'),
                            trigger: "blur"
                        }
                    ],
                    parentId: [
                        {
                            required: true,
                            message: this.$t('professions.plsInputPrarent'),
                            trigger: "blur"
                        }
                    ],
                    root: [
                        {
                            required: true,
                            message: this.$t('professions.plsChooseCategory'),
                            trigger: "blur"
                        }
                    ]
                },
                tabFocus: 0,
                viewData: [],
                showCategoryDialog: false,
                showFirstLevelDialog: false,
                showSecondLevelDialog: false,
                editId: "",
                importUrl: "",
                uploadTittle: ""
            };
        },
        created() {
            this.$store.getters.getFuncUnderPage(this.$route.path).then(funcs => {
                this.functions = funcs;
            });
        },
        mounted: function () {
            this.init();
        },
        methods: {
            init() {
                this.queryForm.level = 3;
                this.queryForm.type = this.type;
                this.queryForm.page = false;
                this.postForm.type = this.type;
                this.getTabs();
                this.getCategories();
                this.onSubmit();
            },
            onSubmit() {
                if (this.tabFocus) {
                    this.queryForm.level = this.tabFocus;
                } else {
                    this.queryForm.level = 3;
                }
                this.getDataList();
            },
            reset() {
                this.queryForm.condition = "";
                this.queryForm.keyword = "";
                this.queryForm.levelOneId = null;
                this.queryForm.levelOneIdList = [];
                this.queryForm.levelTwoId = null;
                this.queryForm.levelTwoIdList = [];
                this.queryForm.parentId = null;
                this.queryForm.parentIdList = [];
                this.queryForm.status = null;//是否生效
                this.options.queryOneLevels.length = 0;
                this.onSubmit()
            },
            resetTwoLevel() {
                this.queryForm.condition = "";
                this.queryForm.keyword = "";
                this.queryForm.parentId = null;
            },
            getDataList(isFirstPage = true) {
                if (isFirstPage) {
                    this.queryForm.pageNum_ = 1;
                }
                getMajorList(this.queryForm).then(res => {
                    if (res.code == 200) {
                        this.viewData = res.data.list;
                        this.queryForm.total_ = res.data.total_;
                    }
                });
            },
            handleSizeChange(val) {
                this.queryForm.pageSize_ = val;
                this.getDataList();
            },
            handleCurrentChange(val) {
                this.queryForm.pageNum_ = val;
                this.getDataList(false);
            },
            //切换Tab
            changeTab(val) {
                this.reset();
                this.tabFocus = val;
                this.queryForm.page = val == 0 ? false : null;
                this.getCategories();
                if (val == 3) {
                    this.getOneLevels();
                }
                this.onSubmit();
            },
            getTabs(){
                if(!this.global.isAdmin){
                    this.tabs = [];
                    this.tabFocus = 0;
                }
            },
            getCategories() {
                let data = {
                    level: 1,
                    type: this.type,
                    page: false
                };
                getAllMajorList(data).then(res => {
                    if (res.code == 200) {
                        this.options.categories = res.data.list.map(c => {
                            return {
                                label: c.name,
                                value: c.id
                            };
                        });
                    }
                });
            },
            changeCategory(val) {
                this.queryForm.levelTwoId = "";
                this.getOneLevels(val);
            },
            getOneLevels(val) {
                let data = {
                    level: 2,
                    page: false,
                    type: this.type
                };
                if (val) {
                    data.parentIdList = val;
                }
                getAllMajorList(data).then(res => {
                    if (res.code == 200) {
                        this.options.queryOneLevels = res.data.list.map(c => {
                            return {
                                label: c.name,
                                value: c.id
                            };
                        });
                    }
                });
            },

            //绑定表单的一级学科
            changeFormCategory(val) {
                this.postForm.parentId = null;
                this.bindOneLevels(val);
            },
            bindOneLevels(val) {
                let data = {
                    level: 2,
                    type: this.type,
                    page: false,
                    parentId: val
                };
                getAllMajorList(data).then(res => {
                    if (res.code == 200) {
                        let result = res.data.list.map(c => {
                            return {
                                label: c.name,
                                value: c.id
                            };
                        });
                        this.options.FormOneLevels = result;
                    }
                });
            },
            //新建门类
            create() {
                this.dialogTitle = this.$t('common.btn.add');
                switch (this.tabFocus) {
                    case 0:
                        break;
                    case 1:
                        this.showCategoryDialog = true;
                        break;
                    case 2:
                        this.showFirstLevelDialog = true;
                        break;
                    case 3:
                        this.showSecondLevelDialog = true;
                        break;
                }
            },
            //取消对话框
            cancel() {
                this.dialogTitle = '';
                switch (this.tabFocus) {
                    case 0:
                        break;
                    case 1:
                        this.showCategoryDialog = false;
                        break;
                    case 2:
                        this.showFirstLevelDialog = false;
                        break;
                    case 3:
                        this.showSecondLevelDialog = false;
                        break;
                }
            },
            resetFormValidate() {
                this.$refs["postForm" + this.tabFocus].clearValidate();
                this.postForm.code = "";
                this.postForm.name = "";
                this.postForm.nameEn = "";
                this.postForm.parentId = null;
                this.postForm.remark = "";
                this.postForm.remarkEn = "";
                this.postForm.status = null;
                this.postForm.root = "";
                this.editId = "";
                this.options.FormOneLevels.length = 0;
            },
            //通用编辑
            commonEdit(row) {
                this.editId = row.id;
                this.dialogTitle = this.$t('common.btn.edit');
                getMajorById(row.id)
                    .then(res => {
                        if (res.code == 200) {
                            this.postForm.code = res.data.code;
                            this.postForm.name = res.data.name;
                            this.postForm.nameEn = res.data.nameEn;
                            this.postForm.remark = res.data.remark;
                            this.postForm.status = res.data.status;
                            this.postForm.parentId = res.data.parentId;
                            switch (this.tabFocus) {
                                case 1:
                                    this.showCategoryDialog = true;
                                    break;
                                case 2:
                                    this.showFirstLevelDialog = true;
                                    break;
                                case 3:
                                    this.showSecondLevelDialog = true;
                                    break;
                            }
                            if (res.data.level == 3) {
                                this.postForm.root = res.data.grandId;
                                return res.data.grandId;
                            }
                        }
                    })
                    .then(id => {
                        if (id) {
                            this.bindOneLevels(id);
                        }
                    });
            },
            //通用保存
            commonSave() {
                this.$refs["postForm" + this.tabFocus].validate(valid => {
                    if (!valid) {
                        return;
                    }
                    this.postForm.level = this.tabFocus;
                    let fn = res => {
                        if (res.code == 200) {
                            this.getDataList();
                            this.cancel();
                        }
                    };
                    if (this.editId) {
                        let data = _.cloneDeep(this.postForm);
                        data.id = this.editId;
                        editMajor(data).then(fn);
                    } else {
                        addMajor(this.postForm).then(fn);
                    }
                });
            },
            //通用删除，可删除门类，一级、二级
            commonDelete(row) {

                // this.$confirm(
                //     this.$t("message.deleteConfirm"),
                //     this.$t("message.prompt"),
                //     {
                //         confirmButtonText: this.$t("message.confirm"),
                //         cancelButtonText: this.$t("message.cancel"),
                //         type: "warning"
                //     }
                this.$delConfirm().then(() => {
                    deleteMajor({ids: [row.id]}).then(res => {
                        if (res.code == 200) {
                            this.$message({
                                type: "success",
                                message: this.$t("message.deleteSuccess")
                            });
                            this.getDataList();
                        }
                    });
                });
            },
            handleExport() {
                let loading = this.$loading({
                    lock: true,
                    text: "Loading",
                    spinner: "el-icon-loading",
                    background: "rgba(255, 255, 255, .48)"
                });
                exportMajor(this.queryForm).then(res => {
                    if (res.code == 200) {
                        this.delayRequest(res.data.key, loading);
                    } else {
                        loading.close();
                    }
                });
            },
            async delayRequest(key, loading) {
                await new Promise(resolve => setTimeout(resolve, 5000));
                getReportStatus(key)
                    .then(res => {
                        if (res.code == 200 && res.data.status) {
                            loading.close();
                            let path =
                                "/api/baseresservice/nationalMajor/export?path=" +
                                res.data.path;
                            window.open(path);
                        } else {
                            this.delayRequest(key, loading);
                        }
                    })
                    .catch(ex => {
                        this.delayRequest(key, loading);
                    });
            },
            labelHead(h, {column, $index}) {
                return h("span", {attrs: {title: column.label}}, [
                    column.label
                ]);
            },

            importSubjectRecord(type) {
                if (type == "in") {
                    this.uploadTittle = this.$t('professions.importNationalSubject');
                    this.$refs.uploadDialog.open();
                    this.importUrl = "/api/baseresservice/nationalMajor/importMaintenanceSubjects?level=1&type=1"
                } else if (type == "out") {
                    window.location.href = downloadSubjectUrl;
                }
            },
            importFristRecord(type) {
                if (type == "in") {
                    this.uploadTittle = this.$t('professions.importFirstLevelDisciplines');
                    this.$refs.uploadDialog.open();
                    this.importUrl = "/api/baseresservice/nationalMajor/importMaintenanceSubjects?level=2&type=1"
                } else if (type == "out") {
                    window.location.href = downloadFristUrl;
                }
            },
            importSecondRecord(type) {
                if (type == "in") {
                    this.uploadTittle = this.$t('professions.importSecondaryDisciplines');
                    this.$refs.uploadDialog.open();
                    this.importUrl = "/api/baseresservice/nationalMajor/importMaintenanceSubjects?level=3&type=1"
                } else if (type == "out") {
                    window.location.href = downloadSecondUrl;
                }
            },

            // downSubjectFiles() {
            //     window.location.href = downloadSubjectUrl;
            // },
            // importSubjectFiles() {
            //     this.uploadTittle = this.$t('professions.importNationalSubject');
            //     this.$refs.uploadDialog.open();
            //     this.importUrl = "/api/baseresservice/nationalMajor/importMaintenanceSubjects?level=1&type=1"
            // },
            // downFristFiles() {
            //     window.location.href = downloadFristUrl;
            // },
            // importFristFiles() {
            //     this.uploadTittle = this.$t('professions.importFirstLevelDisciplines');
            //     this.$refs.uploadDialog.open();
            //     this.importUrl = "/api/baseresservice/nationalMajor/importMaintenanceSubjects?level=2&type=1"
            // },
            // downSecondFiles() {
            //     window.location.href = downloadSecondUrl;
            // },
            // importSecondFiles() {
            //     this.uploadTittle = this.$t('professions.importSecondaryDisciplines');
            //     this.$refs.uploadDialog.open();
            //     this.importUrl = "/api/baseresservice/nationalMajor/importMaintenanceSubjects?level=3&type=1"
            // },
            clearFiles() {
                this.$refs.upload.clearFiles();
                this.fileList = [];
            },
            uploadFile() {
                if (_.isEmpty(this.fileList)) {
                    this.$message({
                        type: "info",
                        message: this.$t("message.selectFilePlease")
                    });
                    return;
                }
                this.$refs.upload.submit();
            },
            uploadDone(res, file, fileList) {
                if (res.code == 200) {
                    this.msg = '';
                    this.errorInfo1 = '';
                    this.errorInfo2 = '';
                    this.$refs.uploadDialog.close();
                    this.importCam = null;
                    this.importButton = true;
                    this.$refs.importRef.open();
                    this.delayImportRequest(res.data.key);
                } else {
                    this.$message({
                        type: "error",
                        message: this.$t(
                            "culture.profession.uploadFileParsingError"
                        )
                    });
                    this.clearFiles();
                    this.importButton = false;
                }
            },
            async delayImportRequest(key) {
                await new Promise(resolve => setTimeout(resolve, 5000));
                getimportStatus(key).then(res => {
                    this.importCam = res.data;
                    if (res.code == 200 && res.data.status) {
                        if (res.data.success) {
                            this.$message({
                                type: "success",
                                message: this.$t("common.upload.uploadCompleted")
                            });
                            this.importCam = {
                                doneCount: 1,
                                updateCount: 1,
                                total: 2
                            };
                            this.importCam.total = this.importCam.doneCount + this.importCam.updateCount;
                            this.fontStyle = 'font-size: 15px;color: #606266;';
                            const total = res.data.total;
                            const doneCount = res.data.doneCount ? res.data.doneCount : 0;
                            const updateCount = res.data.updateCount ? res.data.updateCount : 0;
                            this.msg = this.$t("common.upload.total") + total + this.$t("common.upload.num") + "，" +
                                this.$t("common.upload.success") + doneCount + this.$t("common.upload.num") + "，" +
                                this.$t("common.upload.update") + updateCount + this.$t("common.upload.num") + "，" +
                                this.$t("common.upload.fialed") + (total - doneCount - updateCount) + this.$t("common.upload.num");
                            if ((doneCount || updateCount) && (total - doneCount - updateCount)) {
                                this.errorInfo1 = this.$t("common.upload.errorInfo1");
                                this.errorInfo2 = this.$t("common.upload.errorInfo2");
                            }
                            if (doneCount === 0 && updateCount === 0) {
                                this.$message({
                                    type: "error",
                                    message: this.$t("common.upload.errorInfo")
                                });
                                this.errorInfo1 = this.$t("common.upload.errorInfo1");
                                this.errorInfo2 = this.$t("common.upload.errorInfo2");
                            }
                        } else {
                            this.importCam = {
                                doneCount: 1,
                                updateCount: 1,
                                total: 2
                            };
                            this.$refs.importRef.close();
                            this.$message({
                                type: "error",
                                message: res.data.msg
                            });
                            // this.fontStyle = 'font-size: 15px;color: red'
                            // this.msg = res.data.msg;
                        }

                    } else {
                        this.delayImportRequest(key, loading);
                    }
                }).catch(ex => {
                    this.delayImportRequest(key, loading)
                });
            },
            onError(err, file, fileList) {
                let msg = JSON.parse(err.message);
                this.$message({
                    type: "error",
                    message: msg.message
                });
            },
            beforeRemove(file, fileList) {
                return this.$confirm(this.$t("common.upload.remove") + ` ${file.name}？`);
            },
            beforeAvatarUpload(file) {
                let extName = file.name
                    .substring(file.name.indexOf(".") + 1)
                    .toLowerCase()
                    .trim();
                if (!(extName === "xls" || extName === "xlsx")) {
                    this.$message({
                        type: "error",
                        message: this.$t("common.upload.unSupportesFileType")
                    });
                    this.clearFiles();
                    return;
                }
                if (!(file.size / 1024 / 1024 < 3)) {
                    this.$message({
                        type: "error",
                        message: this.$t("common.upload.sizeLimit3M")
                    });
                    this.clearFiles();
                    return;
                }
            },
            closeFresh() {
                this.onSubmit();
            },
        }
    };
</script>
<style lang="scss" scoped>
  .tab {
    margin: 20px 0;
    overflow: auto;
    font-size: 18px;
    color: #999;
    cursor: pointer;

    div {
      float: left;
    }

    .split {
      margin: 0 20px;
    }

    .current {
      color: #262626;
    }
  }
</style>
